
// Simple Web Get and Word Count  
// http://learnfsharp.blogspot.com

// turn on lightweight syntax -- whitespace matters & no tabs
#light

// import to save type System.Net.Web... and System.IO.Stream.....
open System;;
open System.IO;;
open System.Net;;

/// Define a function to fetch a web page
let fetch(url: string) = 
 let req = WebRequest.Create(url)
 let resp = req.GetResponse()
 let stream = resp.GetResponseStream()
 let reader = new StreamReader(stream)
 let html = reader.ReadToEnd()
 resp.Close()
 html;;

// Use .NET to grab a copy of Henry Ernest Dudeney's Amusements in Mathematics  1917 
let url = "http://www.gutenberg.org/files/16713/16713.txt";;
let book = fetch(url);;

// looks like we got it....we could print it
// printf "%s" book;;

// count the number of words in the given txt
let wc txt = 
 let w = String.split [' '] txt
 let n = w.Length
 n;;

// I put a readline in at the end for those of you using VS, you have a change to see the console output
printfn "Word count is %d" (wc book);;
System.Console.ReadLine();;